package com.ycsj.common.mapchart;


import android.graphics.Canvas;
import android.graphics.Path;

import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.renderer.XAxisRenderer;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

/**
 * Created by Administrator on 2018/1/25 0025.
 */

public class MyXAxisRenderer extends XAxisRenderer {
    private MyLineChart myLineChart;

    public MyXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans, MyLineChart myLineChart) {
        super(viewPortHandler, xAxis, trans);
        this.myLineChart = myLineChart;
    }

    // x轴垂直线
    @Override
    public void renderGridLines(Canvas c) {
        //源码拷贝过来
        if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled())
            return;

        int clipRestoreCount = c.save();
        c.clipRect(getGridClippingRect());

        //if保证mRenderGridLinesBuffer长度为X轴标签数乘以2（mAxis.mEntryCount为label数）
        if (mRenderGridLinesBuffer.length != mAxis.mEntryCount * 2) {
            mRenderGridLinesBuffer = new float[mXAxis.mEntryCount * 2];
        }
        float[] positions = mRenderGridLinesBuffer;

        //mEntries数组里面装的是x轴setValueFormatter里的value中从最小到大开始取的值
        for (int i = 0; i < positions.length; i += 2) {
            positions[i] = mXAxis.mEntries[i / 2];
            positions[i + 1] = mXAxis.mEntries[i / 2];
        }

       /* //用所有矩阵变换点数组。非常重要：保持矩阵顺序“值触摸偏移”时转化。
        mTrans.pointValuesToPixel(positions);*/

        //设置画笔属性
        setupGridPaint();

        Path gridLinePath = mRenderGridLinesPath;
        gridLinePath.reset();

        for (int i = 0; i < positions.length; i += 2) {
            /*
             *                        最后一个坐标X坐标值-第一个坐标X坐标值
             * X轴竖线X坐标 = （---------------------------------------------）*（i/2）+第一个坐标X坐标值
             *                                  label-1
             */
            //下面4行是调整的代码


            //第一个坐标X坐标值
            float fX = myLineChart.getData().getDataSets().get(0).getEntryForIndex(0).getX();
            //最后一个坐标X坐标值
            float eX = myLineChart.getData().getDataSets().get(0).getEntryForIndex(myLineChart.getData().getEntryCount() - 1).getX();
            positions[i] = ((eX - fX) / (mXAxis.mEntryCount - 1)) * (i / 2) + (fX);
            mTrans.pointValuesToPixel(positions);
            //根据positions数组里的值画出X轴竖线
            drawGridLine(c, positions[i], positions[i + 1], gridLinePath);
        }

        c.restoreToCount(clipRestoreCount);

    }


    @Override
    protected void drawLabels(Canvas c, float pos, MPPointF anchor) {

        //把代码复制过来
        final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
        boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();

        float[] positions = new float[mXAxis.mEntryCount * 2];

        for (int i = 0; i < positions.length; i += 2) {

            // only fill x values
            if (centeringEnabled) {
                positions[i] = mXAxis.mCenteredEntries[i / 2];
            } else {
                positions[i] = mXAxis.mEntries[i / 2];
            }
        }

        // mTrans.pointValuesToPixel(positions);

        for (int i = 0; i < positions.length; i += 2) {

            /*
             *                  最后一个坐标X坐标值-第一个坐标X坐标值
             * X轴竖线X坐标 = （---------------------------------------------）*（i/2）+第一个坐标X坐标值
             *                                  label-1
             */
            //下面四行是修改后的代码

            //第一个坐标X坐标值
            float fX = myLineChart.getData().getDataSets().get(0).getEntryForIndex(0).getX();
            //最后一个坐标X坐标值
            float eX = myLineChart.getData().getDataSets().get(0).getEntryForIndex(myLineChart.getData().getEntryCount() - 1).getX();
            float x = positions[i] = ((eX - fX) / (mXAxis.mEntryCount - 1)) * (i / 2) + (fX);
            mTrans.pointValuesToPixel(positions);
            if (mViewPortHandler.isInBoundsX(positions[i])) {

                String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);

                if (mXAxis.isAvoidFirstLastClippingEnabled()) {

                    // avoid clipping of the last mXAxis.mEntryCount-1为x轴标签数
                    if (i == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1) {
                        float width = Utils.calcTextWidth(mAxisLabelPaint, label);

                        if (width > mViewPortHandler.offsetRight() * 2
                                && x + width > mViewPortHandler.getChartWidth())
                            positions[i] -= width / 2;
                        // avoid clipping of the first
                    } else if (i == 0) {

                        float width = Utils.calcTextWidth(mAxisLabelPaint, label);
                        positions[i] += width / 2;
                    }
                }
                drawLabel(c, label, positions[i], pos, anchor, labelRotationAngleDegrees);
            }
        }

    }
}
